<template>
  <div>
    <el-row>
      <el-col :span="15">
        <el-form
         :model="formData"
          ref="elForm"
            class="form-body medias-flash"
            label-suffix="："
            v-loading="loading"
         :rules="rules" label-width="160px">
          <!-- <el-form-item label="所属广告公司：" prop="companyName">
          <el-input 
            v-model=" formData.companyName"
            autocomplete="off"
            placeholder="请输入广告公司名称"
             :style="{ width: '100%' }"
          ></el-input>
        </el-form-item> -->
          <el-form-item label="物料名称" prop="name">
            <el-input
              v-model="formData.name"
              autocomplete="off"
              placeholder="请输入物料名称"
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
          <!--  -->
         <el-form-item label="Flash文件" prop="flash">   
           <el-upload  v-loading="flashLoading"
                    class="upload-demo"
                    action="index.php?module=API&method=AdsPlace.transfile&format=json"
                    :show-file-list="false"
                    :on-success="handleFlashChange"
                    :before-upload="beforeFlashUpload"
                    :on-error="onFlashUploadError"
                     accept=".swf"
                    >
                    <el-button type="primary" icon="el-icon-upload" size="mini"
                        >点击上传</el-button
                    >
                    <label >支持Flash格式:.swf</label>
                    
                    </el-upload>
                    <!-- 上传图片结果显示url -->
            <div v-if="formData.flash" class="word-break">
                      <a> {{formData.flash}} </a></div>

            <!-- <el-upload
              class="upload-demo"            
              action="index.php?module=API&method=AdsPlace.transfile&format=json"
              :on-success="handleSuccess"
              v-bind:on-preview="handlePreview"
               v-bind:on-remove="handleRemove"
              :file-list="formData.images"
              :show-file-list="true"
              :on-exceed="handelLimit"
              :limit="1"
              accept=".swf"
              list-type="picture"             
            >
              <el-button type="primary" icon="el-icon-upload" size="mini"
                >上传图片</el-button
              >
              <label class="in_pictureSupport">支持Flash格式:.swf</label>               
            </el-upload> -->
          </el-form-item>
          <!--  -->
           <el-form-item label="尺寸" prop="width">
            <div>
              <div>
                <span>宽度：</span>
                <el-input
                  v-model=" formData.width"
                  autocomplete="off"                  
                  :style="{ width: '20%' }"
                ></el-input>
                <span class="in_picturePx1"> px</span>
                <span class="in_picture">高度：</span>
                <el-input
                  v-model=" formData.height"
                  autocomplete="off"                 
                  :style="{ width: '20%' }"
                ></el-input>
                <span class="in_picturePx2"> px</span>
              </div>
            </div>
          </el-form-item>
          <!--  -->
           <!--  -->
          <el-form-item label="Flash背景" prop="flashbg">
            <el-radio-group v-model="formData.flashbg" size="medium">
              <el-radio
                v-for="(item, index) in bgColorOptions"
                :key="index"
                :label="item.value"
                :disabled="item.disabled"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <!--  -->
          <!--  -->
          <el-form-item label="目标窗口" prop="target">
            <el-radio-group v-model="formData.target" size="medium">
              <el-radio
                v-for="(item, index) in targetOptions"
                :key="index"
                :label="item.value"
                :disabled="item.disabled"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <!--  -->
           <el-form-item label="点击监控" prop="trackClick">
        <el-checkbox-group v-model="formData.trackClick" size="medium">
          <el-checkbox v-for="(item, index) in trackClickOptions" :key="index" :label="item.value"
            :disabled="item.disabled">{{item.label}}</el-checkbox>
        </el-checkbox-group>
      </el-form-item>
          <!--  -->
            <el-form-item label="点击链接" prop="linkurl">
            <el-input
              v-model="formData.linkurl"
              autocomplete="off"
              placeholder=""
              :style="{ width: '100%' }"
            ></el-input>
          </el-form-item>
          <!--  -->
        <el-form-item label="后备图片" prop="backupPicTag">
        <el-checkbox-group v-model="backupPicTag" size="medium">
          <el-checkbox v-for="(item, index) in backupPicTagOptions" :key="index" :label="item.value"
            :disabled="item.disabled">{{item.label}}</el-checkbox>
        </el-checkbox-group>
        <!-- 上传图片 -->
        <el-row v-if="showBackupPicTag" v-loading="flashpicLoading">
                <el-col :span="24"  class="dropPanel mt2">
                    <el-col :span="24">
                    <el-form-item label="图片文件" prop="flashpic">
                        <el-upload
                                class="upload-demo"
                                action="index.php?module=API&method=AdsPlace.transfile&format=json"
                                :show-file-list="false"
                                :on-success="handleUploadChange"
                                :before-upload="beforeUpload"
                                :on-error="onUploadError"
                               >
                                <el-button type="primary" icon="el-icon-upload" size="mini"
                                    >点击上传</el-button
                                >
                                <label >支持jpg、gif、png格式</label>
                                <!-- <p class="in_pictureURL">使用网络图片URL</p> -->
                                </el-upload>
                                <!-- 上传图片结果显示url -->
                        <div v-if="formData.flashpic" class="word-break">
                                 <a> {{formData.flashpic}} </a></div>
                   </el-form-item>
                             
                     
                    </el-col>
<!--  -->           
                     <el-col :span="24">
                    <el-form-item label="图片尺寸" prop="flashpicwidth">
                                <div>
                                <div>
                                    <span>宽度：</span>
                                    <el-input
                                    v-model=" formData.flashpicwidth"
                                    autocomplete="off"
                                    size="mini"
                                    :style="{ width: '20%' }"
                                    ></el-input>
                                    <span> px</span>
                                    <span class="in_picture">  高度：</span>
                                    <el-input
                                    v-model="formData.flashpicheight"
                                    autocomplete="off"
                                    size="mini"
                                    :style="{ width: '20%' }"
                                    ></el-input>
                                    <span> px</span>
                                </div>
                                </div>
                            </el-form-item>
                     </el-col>
                </el-col>
        </el-row>
      </el-form-item>
          <!--  -->

          <el-form-item label="展现监控链接" prop="trackurl">
            <el-input
              v-model="formData.trackurl"
              placeholder="请输入展现监控链接"
              clearable
              :style="{ width: '100%' }"
            >
            </el-input>
          </el-form-item>
          <!--  -->
          <el-form-item size="large" class="form-submit-panel">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">关闭</el-button>
          </el-form-item>
          <!--  -->
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  components: {},
  props: ['mediasRow'],
  data() {
    return {
      formData: {
        id: 0,
        type: 'flash',
        name: "",
        flash: '',
        width: "",
        height: "",
        flashbg: 2,
        flashpic:'',
        flashpicwidth:0,
        flashpicheight:0,
        linkurl: "",
        target: 1,
        trackurl: "",
        trackClick: [],
        
      },
      upimages:[],
      backupPicTag:[], // 显示添加 备用图片
      flashpicLoading: false, //上传备用图片loading'
      flashLoading: false, // 上传flash 素材loading 效果
      loading: false, // 提交表单的loading 
      rules: {
        name: [
          {
            required: true,
            message: "物料名称不能为空",
            trigger: "change",
          },
        ],
        flash: [
          {
            required: true,
            message: "请上传flash文件",
            trigger: "change",
          },
        ],
          width: [
          {
            required: true,
            message: "请填写宽高",
            trigger: "change",
          },
        ],
          height: [
          {
            required: true,
            message: "请填写宽高",
            trigger: "change",
          },
        ],
        target: [
          {
            required: true,
            message: "目标窗口不能为空",
            trigger: "change",
          },
        ],
        trackurl: [],
      },
      targetOptions: [
        {
          label: "新窗口",
          value: 1,
        },
        {
          label: "原窗口",
          value: 2,
        },
      ],
      bgColorOptions:[
          {
          label: "透明",
          value: 1,
        },
        {
          label: "不透明",
          value: 2,
        }, 
      ],
      trackClickOptions: [{
        "label": "允许对Flash进行点击监控",
        "value": 1
      }],
      backupPicTagOptions: [{
        "label": "Flash无法展现时显示后备图片",
        "value": 1
      }],
    };
  },
  created(){
      if(!this.row  || !this.row.hasOwnProperty("id")){
        return;
      }
      // console.log(this.row)
       // 修改是不是被类型的数据不渲染
      if(this.row.type != 'flash') { return; }

       // 
      if(this.row.content){        
        this.row['flash'] = this.row.content.flash ;
      }
      // 
      for(var key in this.formData){
         if( this.row[key]){
              this.formData[key] = this.row[key];
         }
       }
       //初始化一些选项
       if(this.formData.flashpic){
         this.backupPicTag = [1];
       }
       if(this.row.trackClick==1){
         this.formData.trackClick = [1];
       }
     
  },
  computed:{
     showBackupPicTag(){
        return this.backupPicTag.length>0;
      },
      row: function(){
       
       return this.mediasRow
     }
  },
  methods: {
     // 上传FLash 素材
    handleFlashChange(response, file, fileList){
        this.formData.flash = response.data;
        this.flashLoading = false;
    },
    beforeFlashUpload(file ){
      this.flashLoading=true;
    },
    onFlashUploadError(err, file, fileList){
      this.flashLoading = false;
      this.$message({message:"上传失败", type:"error"})
    },
    // 上传备用图片
    handleUploadChange(response, file, fileList){
        this.formData.flashpic = response.data;
        this.flashpicLoading = false;
    },
    beforeUpload(file ){
      this.flashpicLoading=true;
    },
    onUploadError(err, file, fileList){
      this.flashpicLoading = false;
      this.$message({message:"上传失败", type:"error"})
    },
    //

    submitForm() {
     // 表单处于loading状态 
      

      this.$refs["elForm"].validate((valid) => {
        if (!valid) return;
        this.loading = true;
        // TODO 提交表单
        // console.log(this.formData)
        //  提交数据到接口，更新和新建通过 formData.id > 0 来区别
         PlaceAPI.createMedias(this.formData).then((data)=>{
            this.loading = false;
            this.$message({ message: data.msg, type: "success" });
            // 触发关闭Drawer 侧滑层 关闭
            this.formData.id = data.value;
            // 添加成功返回 data.vaule = 物料id；
            this.$emit("onclosedrawer",{ from: 'submit',row: this.formData})
         })

      });
    },
    resetForm() {
       this.$emit("onclosedrawer",{ from: ''})
    },
  },
};
</script>
